package com.mall.mapper;

import com.mall.domain.po.Address;
import com.mall.domain.vo.AddressVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 勾新杰
 * @version 1.0
 * @date 2024/11/22 21:02
 */
@Mapper
public interface AddressMapper {

    /**
     * 新增地址
     * @param address 地址
     */
    @Insert("insert into address(user_id, receiver_name, receiver_phone, province, city, district, detail_address, is_default, create_time, update_time) " +
            "VALUES (#{userId}, #{receiverName}, #{receiverPhone}, #{province}, #{city}, #{district}, #{detailAddress}, #{isDefault}, #{createTime}, #{updateTime})")
    void insert(Address address);

    /**
     * 根据用户id查询地址
     * @param userId 用户id
     * @return 地址列表
     */
    @Select("select id, user_id, receiver_name, receiver_phone, province, city, district, detail_address, is_default, create_time, update_time" +
            " from address where user_id = #{userId};")
    List<Address> getByUserId(Long userId);

    /**
     * 根据地址id查询地址
     * @param id 地址id
     * @return 地址
     */
    @Select("select id, user_id, receiver_name, receiver_phone, province, city, district, detail_address, is_default, create_time, update_time" +
            " from address where id = #{id} and user_id = #{userId};")
    AddressVO getByIdAndUserId(Long id, Long userId);

    /**
     * 修改地址
     * @param address 地址
     */
    void update(Address address);

    /**
     * 删除地址
     * @param id 地址id
     */
    @Delete("delete from address where id = #{id}")
    void deleteById(Long id);
}
